package com.demo.service.impl;


import java.util.Collections;
import java.util.List;

import com.demo.entity.constants.Constants;
import com.demo.entity.dto.SessionWebUserDto;
import com.demo.entity.enums.ResponseCodeEnum;
import com.demo.entity.po.GroupInfo;
import com.demo.entity.po.GroupMember;

import com.demo.entity.query.GroupInfoQuery;
import com.demo.entity.query.GroupMemberQuery;

import com.demo.entity.vo.PaginationResultVO;

import com.demo.exception.BusinessException;
import com.demo.mappers.GroupInfoMapper;
import com.demo.service.GroupMemberService;

import com.demo.utils.LoginInfoUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.demo.entity.query.SimplePage;

import com.demo.mappers.GroupMemberMapper;

import com.demo.entity.enums.PageSize;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description:小组成员表serviceImpl
 * @Date:2025-03-01
 * @author：author
*/
@Service("groupMemberService")
public class GroupMemberServiceImpl implements GroupMemberService{

	@Resource
	private GroupMemberMapper<GroupMember,GroupMemberQuery> groupMemberMapper;

	@Resource
	private GroupInfoMapper<GroupInfo, GroupInfoQuery> groupInfoMapper;

	/**
	 * 根据条件查询列表
	 */
	public List<GroupMember> findListByParam(GroupMemberQuery query) { 
		return this.groupMemberMapper.selectList(query);
	}

	/**
	 * 根据条件查询数量
	 */
	public Integer findCountByParam(GroupMemberQuery query){
		return this.groupMemberMapper.selectCount(query);
	}

	/**
	 * 分页查询
	 */
	public PaginationResultVO<GroupMember> findListByPage(GroupMemberQuery  query) { 
		int count = this.findCountByParam(query);
		int pageSize = query.getPageSize()==null?PageSize.SIZE15.getSize():query.getPageSize();
		SimplePage page = new SimplePage(query.getPageNo(),count,pageSize);
		query.setSimplePage(page);
		List<GroupMember> list = this.findListByParam(query);
		return new PaginationResultVO(count,page.getPageSize(),page.getPageNo(),page.getPageTotal(),list);
	}

	/**
	 * 新增
	 */
	public Integer add(GroupMember bean) { 
		return this.groupMemberMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	public Integer addBatch(List<GroupMember> listBean) { 
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.groupMemberMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
	public Integer addOrUpdateBatch(List<GroupMember> listBean) { 
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.groupMemberMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 根据MemberId查询
	 */
	public GroupMember getGroupMemberByMemberId(Long memberId) { 
		return this.groupMemberMapper.selectByMemberId(memberId);
	}

	/**
	 * 根据MemberId更新
	 */
	public Integer updateGroupMemberByMemberId(GroupMember bean, Long memberId) { 
		return this.groupMemberMapper.updateByMemberId(bean,memberId);
	}

	/**
	 * 根据MemberId删除
	 */
	public Integer deleteGroupMemberByMemberId(Long memberId) { 
		return this.groupMemberMapper.deleteByMemberId(memberId);
	}


	/**
	 * 查询小组成员
	 * @param query
	 * @return
	 */
	@Override
	public List<GroupMember> loadGroupMember(GroupMemberQuery query) {
		query.setOrderBy("join_time desc");
		List<GroupMember> groupMemberList = groupMemberMapper.selectList(query);
		if (groupMemberList.size() <= Constants.ZERO) {
			throw new BusinessException("您还不是小组成员，无法查看详细信息");
		}
		return groupMemberList;
	}


	// 组长新增小组成员
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void addMemberInfo(List<GroupMember> listBean) {
		SessionWebUserDto webUserDto = LoginInfoUtil.getSessionWebUserDto();
		GroupMember groupMember = null;
		if (listBean.size()<=Constants.ZERO){ // 没有数据
			return;
		}
		groupMember=listBean.get(0);
		GroupInfo groupInfo = null;
		if (groupMember.getGroupId()!=null){
			groupInfo=groupInfoMapper.selectByGroupId(groupMember.getGroupId());
		}
		if (groupInfo==null||!groupInfo.getCreatorId().equals(webUserDto.getUserId())) { // 这个小组组不存在或者不是小组创建人无法添加好友
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		groupMemberMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 删除小组成员信息
	 * @param groupId 小组id
	 * @param userId 用户id
	 */
	@Override
	public void delInfo(Long groupId, Long userId) {
		GroupMemberQuery memberQuery = new GroupMemberQuery();
		memberQuery.setGroupId(groupId);
		memberQuery.setUserId(userId);
		Integer count = groupMemberMapper.deleteByParam(memberQuery);
		if (count<=Constants.ZERO){
			throw new BusinessException("已经不在小组了");
		}
	}
}